<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Log;
use App\Http\Controllers\Auth\YRDauth;
use Overtrue\Wechat\Auth;
use Illuminate\Support\Facades\Session;
use App\Tucao;

class TucaoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
        $data = [];
        $tucao_content = $request->get('tucao_content');
        $nickname = $request->get('nickname');
        $sex = $request->get('sex');
        $openid = $request->get('openid');
        $headimgurl = $request->get('headimgurl');
        $times = 1;
        $create_time = time();
        if(trim($tucao_content) == '') {
            $content = DB::table('tucaos')->get();

            Log::info(json_encode($content));
            return json_encode($content);
        }

        $same = Tucao::where('content', $tucao_content)->get();

        if(!empty($same[0])) {
            Tucao::where('content', $tucao_content)->update(['times'=> $same[0]->times + 1]);
        }else {

            // 入库
            $id = DB::table('tucaos')->insertGetId(
                ['content'=> $tucao_content,'openid'=>$openid, 'nickname'=> $nickname, 'sex'=>$sex, 'headimgurl'=>$headimgurl , 'times'=>$times, 'create_time'=> $create_time ]
            );
        }
        if(!empty($id) || !empty($same)) {
            $content = DB::table('tucaos')->get();

            Log::info(json_encode($content));
            return json_encode($content);
        }else {
            return false;
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show(Auth $auth)
    {
        //
        $data = [];
//        $data['isauth'] = 1;
//        $data['sex'] = 1;
//        $data['nickname'] = 'guanyue';
//        $data['openid'] = 'abcdefg';
//        $data['headimgurl'] = 'headdfds.jpg';

        // auth start
        $user = Session::get('loggin_user');
        if( empty($user) ) {
            $user = $auth->authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE');
            Session::put('loggin_user', $user);
            Log::info('session赋值');
        }

        $userInfo = json_decode($user);
        Log::info('获取用户信息了：'. $user);
        $isValid = $this->checkUserValid($userInfo->openid);
        if(!$isValid) {
            $data['isauth'] = 0;
            return View('tucao_notauth', $data);
        }else {
            $data['isauth'] = 1;
        }

        $data['sex'] = $userInfo->sex;
        $data['nickname'] = $userInfo->nickname;
        $data['openid'] = $userInfo->openid;
        $data['headimgurl'] = $userInfo->headimgurl;
        // auth end

        return View('tucao', $data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 检查用户是否有效
     * @param $openid
     */

    public function checkUserValid($openid) {
        // 查找数据库，是否绑定过
        $find_result = DB::table('user_record')->where('openid', $openid)->get();
        if(empty($find_result)) {
            return false;
        }else {
            return true;
        }
    }
}
